﻿//创建时间: 2010-10-11
//创建作者: 李 城
//功能说明: 内容模型 字段管理
//最后修改: 2010-10-11 修改  DeleteInfoById 使其调用  Idal.DeleteInfoById 实现修改数据表字段的功能
//最后修改: 2010-10-21李晓忠  增加GetWithTableNameListByCondition功能
//最后修改: 2010-11-16李晓忠  增加获取标题字段功能
//最后修改: 2010-11-19 李晓忠  添加获取主内容字段
//最后修改: 2010-11-25 李晓忠  添加获取搜索字段
//最后修改: 2011-1-25 李晓忠   增添了缓存的功能
using System;
using System.Collections.Generic;
using System.Text;


using TRPOP.CMS.Model;
using TRPOP.CMS.DALFactory;
using TRPOP.CMS.Plugin;
using TRPOP.Common;

namespace TRPOP.CMS.Business
{
    public class ModelFieldManage
    {

        private IModelFieldDAL Idal;
        public ModelFieldManage()
        {
            Idal = DataAccess.CreateModelFieldDAL();
        }

        #region 当编辑时删除所有已之相关的缓存
        /// <summary>
        /// 当编辑时删除所有已之相关的缓存
        /// </summary>
        /// <param name="oEnumSubmitResult"></param>
        /// <returns></returns>
        public EnumSubmitResult RemoveCacheWhenEdit(EnumSubmitResult oEnumSubmitResult)
        {
            if (oEnumSubmitResult == EnumSubmitResult.Success)
            {
                ICacheAccess.GetInstance().RemoveByRegexp(CacheKeys.CacheReg_ModelAll);
            }
            return oEnumSubmitResult;
        }
        #endregion

        #region 保存 Save
        ///	<summary>
        ///	保存
        ///	</summary>
        ///	<param name="ModelFieldInfo"></param>
        ///	<returns>EnumSubmitResult</returns>
        public EnumSubmitResult Save(ModelFieldInfo info)
        {
            return RemoveCacheWhenEdit(Idal.Save(info));
        }

        #endregion

        #region 获取实体 GetInfoById
        ///	<summary>
        ///	获取实体
        ///	</summary>
        ///	<param name="modelFieldId"></param>
        ///	<returns>ModelFieldInfo</returns>
        public ModelFieldInfo GetInfoById(int id)
        {
            //string strCacheKey = string.Format(CacheKeys.Model_GetInfoById, id);
            //if (mICache.IsExist(strCacheKey) == false)
            //{
            //    ModelFieldInfo oModelFieldInfo = Idal.GetInfoById(id);
            //    mICache.Save(strCacheKey, oModelFieldInfo, CacheKeys.ModelTimeSecond);
            //    return oModelFieldInfo;
            //}
            //return (ModelFieldInfo)mICache.Read(strCacheKey);
            return CacheHelper.GetByICache<ModelFieldInfo>(
                    string.Format(CacheKeys.Model_GetInfoById, id),
                    CacheKeys.ModelTimeSecond,
                    ICacheAccess.GetInstance(),
                    () =>
                    {
                        return Idal.GetInfoById(id);
                    }
                );
        }
        #endregion

        #region 删除 DeleteInfoById
        ///	<summary>
        ///	删除
        ///	</summary>
        ///	<param name="id"></param>
        ///	<returns>EnumSubmitResult</returns>
        public EnumSubmitResult DeleteInfoById(int id)
        {
            return RemoveCacheWhenEdit(Idal.DeleteInfoById(id));
        }
        #endregion

        #region 删除 DeleteInfoByIds
        ///	<summary>
        ///	删除
        ///	</summary>
        ///	<param name="ids"></param>
        ///	<returns>EnumSubmitResult</returns>
        public EnumSubmitResult DeleteInfoByIds(string ids)
        {
            return RemoveCacheWhenEdit(Idal.DeleteInfoByIds(ids));
        }
        #endregion

        #region 自定义列表（无分页） GetListByCondition
        ///	<summary>
        ///	自定义列表（无分页）
        ///	</summary>
        ///	<param name="topn">前N条，为0时表示所有符合条件的</param>
        ///	<param name="condition">自定义条件，要以And开头</param>
        ///	<param name="orderby">排序，不需要Order by </param>
        ///	<returns>List<ModelFieldInfo></returns>
        public List<ModelFieldInfo> GetListByCondition(int topn, string condition, string orderby)
        {
            return Idal.GetListByCondition(topn, condition, orderby);
        }
        #endregion

        #region 自定义列表（分页） GetListByPager
        ///	<summary>
        ///	自定义列表（分页）
        ///	</summary>
        ///	<returns>List<ModelFieldInfo></returns>
        public List<ModelFieldInfo> GetListByPager(int pageNumber, int pageSize, string condition, string orderby, out int recordCount)
        {
            return Idal.GetListByPager(pageNumber, pageSize, condition, orderby, out  recordCount);
        }
        #endregion

        #region 通过ModelId 获取模型字段 GetWithTableNameListByModelId
        /// <summary>
        ///通过ModelId 获取模型字段 GetWithTableNameListByModelId
        /// </summary>
        /// <param name="topn"></param>
        /// <param name="condition"></param>
        /// <param name="orderby"></param>
        /// <returns></returns>
        public List<ModelFieldInfoWithTableName> GetWithTableNameListByModelId(int ModelId)
        {
            //return  Idal.GetWithTableNameListByModelId(ModelId);
            //string strCacheKey = string.Format(CacheKeys.Model_WithTableNameListByModelId, ModelId);
            //if (mICache.IsExist(strCacheKey) == false)
            //{
            //    List<ModelFieldInfoWithTableName> olist =  Idal.GetWithTableNameListByModelId(ModelId);
            //    mICache.Save(strCacheKey,olist, CacheKeys.ModelTimeSecond);
            //    return olist;
            //}
            //return (List<ModelFieldInfoWithTableName>)mICache.Read(strCacheKey);

            return CacheHelper.GetByICache<List<ModelFieldInfoWithTableName>>(
                    string.Format(CacheKeys.Model_WithTableNameListByModelId, ModelId),
                    CacheKeys.ModelTimeSecond,
                    ICacheAccess.GetInstance(),
                    () =>
                    {
                        //return  Idal.GetWithTableNameListByModelId(ModelId);
                        List<ModelFieldInfoWithTableName> oList = Idal.GetWithTableNameListByModelId(ModelId);
                        if (oList == null)
                            oList = new List<ModelFieldInfoWithTableName>();
                        return oList;
                    }
                );
             
        }
        #endregion

        #region   通过NodeId 获取模型字段 GetWithTableNameListByNodeId
        /// <summary>
        ///  通过NodeId 获取模型字段 GetWithTableNameListByNodeId
        /// </summary>
        /// <param name="NodeId"></param>
        /// <returns></returns>
         public List<ModelFieldInfoWithTableName> GetWithTableNameListByNodeId(int NodeId)
        {
            
            //string strCacheKey = string.Format(CacheKeys.Model_WithTableNameListByNodeId, NodeId);
            //if (mICache.IsExist(strCacheKey) == false)
            //{
            //    List<ModelFieldInfoWithTableName> olist = Idal.GetWithTableNameListByNodeId(NodeId);
            //    mICache.Save(strCacheKey,olist , CacheKeys.ModelTimeSecond);
            //    return olist;
            //}
            //return (List<ModelFieldInfoWithTableName>)mICache.Read(strCacheKey);
            return CacheHelper.GetByICache<List<ModelFieldInfoWithTableName>>(
                    string.Format(CacheKeys.Model_WithTableNameListByNodeId, NodeId),
                    CacheKeys.ModelTimeSecond,
                    ICacheAccess.GetInstance(),
                    () =>
                    {
                        return Idal.GetWithTableNameListByNodeId(NodeId);
                    }
                );

        }
        #endregion

        #region  模型 列表显示字段列表 GetTableFieldListDisplayListByNodeId
         /// <summary>
         /// 模型 列表显示字段列表 GetTableFieldListDisplayListByNodeId
         /// </summary>
         /// <param name="NodeId"></param>
         /// <returns></returns>
         public List<ModelFieldInfo> GetTableFieldListDisplayListByNodeId(int NodeId)
         {
             /*
              *  之前的代码
              * 
             ICache oICache  =  ICacheAccess.GetInstance();
             string strCacheKey =  string.Format(CacheKeys.Model_TableFieldListDisplayListByNodeId,NodeId);
             if (oICache.IsExist(strCacheKey) == false)
             {
                 List<ModelFieldInfo> olist = Idal.GetTableFieldListDisplayListByNodeId(NodeId);
                 if (olist == null)
                 {
                     olist = new List<ModelFieldInfo>();
                 }
                 oICache.Save(strCacheKey, olist, CacheKeys.ModelTimeSecond);
                 return olist;
             }
             return (List<ModelFieldInfo>)oICache.Read(strCacheKey);
             */
             return CacheHelper.GetByICache<List<ModelFieldInfo>>(
                        string.Format(CacheKeys.Model_TableFieldListDisplayListByNodeId, NodeId),
                        CacheKeys.ModelTimeSecond,
                        ICacheAccess.GetInstance(),
                        () =>
                        {
                            List<ModelFieldInfo> olist = Idal.GetTableFieldListDisplayListByNodeId(NodeId);
                            if (olist == null)
                            {
                                olist = new List<ModelFieldInfo>();
                            }
                            return olist;
                        }
            );
         }
         #endregion

        #region 获取标题字段
        /// <summary>
         /// 获取标题字段
        /// </summary>
        /// <param name="NodeId"></param>
        /// <returns></returns>
         public ModelFieldInfo GetTableTitleFieldByNodeId(int NodeId)
         {
             //string strCacheKey = string.Format(CacheKeys.Model_TableTitleFieldByNodeId, NodeId);
             //if (mICache.IsExist(strCacheKey) == false)
             //{
             //    ModelFieldInfo oModelFieldInfo = Idal.GetTableTitleFieldByNodeId(NodeId);
             //    mICache.Save(strCacheKey, oModelFieldInfo, CacheKeys.ModelTimeSecond);
             //    return oModelFieldInfo;

             //}
             //return (ModelFieldInfo)mICache.Read(strCacheKey);
             return CacheHelper.GetByICache<ModelFieldInfo>(
                     string.Format(CacheKeys.Model_TableTitleFieldByNodeId, NodeId),
                     CacheKeys.ModelTimeSecond,
                     ICacheAccess.GetInstance(),
                     () =>
                     {
                         return Idal.GetTableTitleFieldByNodeId(NodeId);
                     }
                 );
         }
        /// <summary>
         /// 获取标题字段
        /// </summary>
        /// <param name="ModelId"></param>
        /// <returns></returns>
         public ModelFieldInfo GetTableTitleFieldByModelId(int ModelId)
         {
             //string strCacheKey = string.Format(CacheKeys.Model_TableTitleFieldByModelId, ModelId);
             //if (mICache.IsExist(strCacheKey) == false)
             //{
             //    ModelFieldInfo oModelFieldInfo = Idal.GetTableTitleFieldByModelId(ModelId);
             //    mICache.Save(strCacheKey, oModelFieldInfo, CacheKeys.ModelTimeSecond);
             //    return oModelFieldInfo;
             //}
             //return (ModelFieldInfo)mICache.Read(strCacheKey);
             return CacheHelper.GetByICache<ModelFieldInfo>(
                     string.Format(CacheKeys.Model_TableTitleFieldByModelId, ModelId),
                     CacheKeys.ModelTimeSecond,
                     ICacheAccess.GetInstance(),
                     () =>
                     {
                         return Idal.GetTableTitleFieldByModelId(ModelId);
                     }
                 );
         }
        #endregion

        #region 获取主内容字段
         /// <summary>
         /// 获取主内容字段
         /// </summary>
         /// <param name="NodeId"></param>
         /// <returns></returns>
         public ModelFieldInfo GetTableMainFieldByNodeId(int NodeId)
         {
             //string strCacheKey = string.Format(CacheKeys.Model_TableMainFieldByNodeId, NodeId);
             //if (mICache.IsExist(strCacheKey) == false)
             //{
             //    ModelFieldInfo oModelFieldInfo = Idal.GetTableMainFieldByNodeId(NodeId);
             //    mICache.Save(strCacheKey, oModelFieldInfo, CacheKeys.ModelTimeSecond);
             //    return oModelFieldInfo;
             //}
             //return (ModelFieldInfo)mICache.Read(strCacheKey);
             return CacheHelper.GetByICache<ModelFieldInfo>(
                     string.Format(CacheKeys.Model_TableMainFieldByNodeId, NodeId),
                     CacheKeys.ModelTimeSecond,
                     ICacheAccess.GetInstance(),
                     () =>
                     {
                         return Idal.GetTableMainFieldByNodeId(NodeId);
                     }
                 );
         }
         /// <summary>
         /// 获取主内容字段
         /// </summary>
         /// <param name="ModelId"></param>
         /// <returns></returns>
         public ModelFieldInfo GetTableMainFieldByModelId(int ModelId)
         {
             //string strCacheKey = string.Format(CacheKeys.Model_TableMainFieldByModelId, ModelId);
             //if (mICache.IsExist(strCacheKey) == false)
             //{
             //    ModelFieldInfo oModelFieldInfo = Idal.GetTableMainFieldByModelId(ModelId);
             //    mICache.Save(strCacheKey, oModelFieldInfo, CacheKeys.ModelTimeSecond);
             //    return oModelFieldInfo;
             //}
             //return (ModelFieldInfo)mICache.Read(strCacheKey);
             return CacheHelper.GetByICache<ModelFieldInfo>(
                     string.Format(CacheKeys.Model_TableMainFieldByModelId, ModelId),
                     CacheKeys.ModelTimeSecond,
                     ICacheAccess.GetInstance(),
                     () =>
                     {
                         return Idal.GetTableMainFieldByModelId(ModelId);
                     }
                 );
         }
         #endregion

        #region 更新标题字段
         public EnumSubmitResult UpdateTitleFiled(int id)
         {
             return RemoveCacheWhenEdit(Idal.UpdateTitleFiled(id));
         }
         #endregion

        #region 更新主内容字段
         public EnumSubmitResult UpdateMainField(int id)
         {
             return RemoveCacheWhenEdit(Idal.UpdateMainField(id));
         }
         #endregion

        #region 将字段更新到相反的值 1==0 0==1
         public EnumSubmitResult UpdateField(int id, string type)
         {
             return RemoveCacheWhenEdit(Idal.UpdateField(id, type));
         }
        #endregion

        #region 获取搜索字段
         /// <summary>
         /// 获取搜索字段
         /// </summary>
         /// <param name="NodeId"></param>
         /// <returns></returns>
         public List<ModelFieldInfo> GetTableSearchFieldListByNodeId(int NodeId)
         {
             //string strCacheKey = string.Format(CacheKeys.Model_TableSearchFieldListByNodeId, NodeId);
             //if (mICache.IsExist(strCacheKey) == false)
             //{
             //     List<ModelFieldInfo> olist  = Idal.GetTableSearchFieldListByNodeId(NodeId);
             //     mICache.Save(strCacheKey, olist, CacheKeys.ModelTimeSecond);
             //     return olist;
             //}
             //return (List<ModelFieldInfo>)mICache.Read(strCacheKey);
             return CacheHelper.GetByICache<List<ModelFieldInfo>>(
                     string.Format(CacheKeys.Model_TableSearchFieldListByNodeId, NodeId),
                     CacheKeys.ModelTimeSecond,
                     ICacheAccess.GetInstance(),
                     () =>
                     {
                         return Idal.GetTableSearchFieldListByNodeId(NodeId);
                     }
                 );
         }
         #endregion

    }
}
